﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace lianxi2
{
    public class DBHelper
    {
        public static string ConnString = "server=.;database=gongzhuang;Integrated Security=true;";
        public static SqlConnection conn = null;
        private static void InitConnection()
        {
            if (conn == null)
                conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

        }
        //查询方法
        public static DataSet GetDataSet(string sqlstr)
        {
            InitConnection();
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(sqlstr, conn);
            dap.Fill(ds);
            conn.Close();
            return ds;
        }
        //增删改
        public static int ExecuteNonQuery(string str)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(str, conn);
            int result = cmd.ExecuteNonQuery();
            conn.Close();
            return result;
        }
    }
}